# coding:utf-8
import sys

from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QIcon, QDesktopServices, QColor
from PyQt5.QtWidgets import QApplication, QFrame, QHBoxLayout
from qfluentwidgets import (NavigationItemPosition, MessageBox, setTheme, Theme, MSFluentWindow,
                            NavigationAvatarWidget, qrouter, SubtitleLabel, setFont, InfoBadge,
                            InfoBadgePosition, FluentBackgroundTheme)
from qfluentwidgets import FluentIcon as FIF
from ui_form.view.upload_interface import UploadInterface
from ui_form.view.table_interface import TableInterface
from ui_form.view.chart_interface import ChartInterface


class Widget(QFrame):

    def __init__(self, text: str, parent=None):
        super().__init__(parent=parent)
        self.label = SubtitleLabel(text, self)
        self.hBoxLayout = QHBoxLayout(self)

        setFont(self.label, 24)
        self.label.setAlignment(Qt.AlignCenter)
        self.hBoxLayout.addWidget(self.label, 1, Qt.AlignCenter)
        self.setObjectName(text.replace(' ', '-'))


class Window(MSFluentWindow):

    def __init__(self):
        super().__init__()

        # create sub interface
        self.main_interface = UploadInterface()
        self.table_interface = TableInterface()
        self.chart_interface = ChartInterface()

        self.initNavigation()
        self.initWindow()
        
        self.data = None

    def initNavigation(self):
        self.addSubInterface(self.main_interface, FIF.HOME.colored(QColor(1, 159, 170), Qt.blue), '上传界面')
        self.addSubInterface(self.table_interface, FIF.FOLDER.colored(QColor(1, 159, 170), Qt.blue), '表格界面')
        self.addSubInterface(self.chart_interface, FIF.BOOK_SHELF.colored(QColor(1, 159, 170), Qt.blue), '图表界面')
        # self.navigationInterface.addSeparator()

        # add custom widget to bottom
        self.navigationInterface.addWidget(
            routeKey='avatar',
            widget=NavigationAvatarWidget('ROSON', 'ui_form/img/teacher.png'),
            onClick=self.showMessageBox,
            position=NavigationItemPosition.BOTTOM,
        )

    def initWindow(self):
        self.resize(900, 700)
        # self.resize(550, 500)
        self.setWindowIcon(QIcon('ui_form/img/exam.png'))
        self.setWindowTitle('试卷核分系统')

        desktop = QApplication.desktop().availableGeometry()
        w, h = desktop.width(), desktop.height()
        self.move(w//2 - self.width()//2, h//2 - self.height()//2)

    def showMessageBox(self):
        w = MessageBox(
            '支持作者🥰',
            '感谢每一位辛勤付出的老师！',
            self
        )
        w.yesButton.setText('这次一定')
        w.cancelButton.setText('下次一定')

        if w.exec():
            QDesktopServices.openUrl(QUrl("https://afdian.net/a/zhiyiYo"))


if __name__ == '__main__':
    QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
    setTheme(Theme.DARK)
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    app.exec_()
